package cn.com.wangluotongxin.merchants.controller;

import cn.com.wangluotongxin.common.utils.TenantUtils;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.merchants.eureka.form.AddMerchantsRoleEditForm;
import cn.com.wangluotongxin.merchants.eureka.form.AddMerchantsRoleForm;
import cn.com.wangluotongxin.merchants.eureka.form.InitPlatformRoleForm;
import cn.com.wangluotongxin.merchants.service.IMerchantsRoleService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "网络通信- 用户角色服务相关接口")
@RestController
@RequestMapping("/inner/role")
public class InnerRoleController {

    @Autowired
    private IMerchantsRoleService roleService;

    @ApiOperation("商户资源用户角色初始化")
    @PostMapping("/init")
    public AjaxResult init(@RequestBody InitPlatformRoleForm form) {

        roleService.initRole(TenantUtils.get().getKey().toString());
        return AjaxResult.success();
    }

    @ApiOperation("添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AddMerchantsRoleForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getRoleName()), "缺少名称");

        roleService.add(form);
        return AjaxResult.success();
    }

    @ApiOperation("编辑")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody AddMerchantsRoleEditForm form) {
        Assert.notNull(form.getId(), "缺少数据ID");
        Assert.isTrue(StrUtil.isNotBlank(form.getRoleName()), "缺少名称");

        roleService.edit(form);
        return AjaxResult.success();
    }
}
